package main.ServerTest;

import main.JdbcUtils;
import main.Service.ChatMessageService;
import main.ServiceImpli.ChatMessageServiceImpl;
import main.pojo.ChatMessage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class ChatMessageServiceTest {
    private Connection connection;
    private ChatMessageService chatMessageService;

    // 请替换以下数据库连接信息为实际的数据库信息

    @Before
    public void setUp() throws SQLException {
        // 连接数据库
        connection = JdbcUtils.getConnection();
        // 实例化 ChatMessageService
        chatMessageService = new ChatMessageServiceImpl(connection);
    }
    @After
    public void tearDown() throws SQLException {
        // 关闭数据库连接
        if (connection != null) {
            connection.close();
        }
    }
    @Test
    public void testGetChatMessageByID() {
        int messageID = 1;
        ChatMessage message = chatMessageService.getChatMessageByID(messageID);
        System.out.println("Chat Message with ID " + messageID + ": " + Arrays.toString(message.getPhoto()));
        // 添加断言判断
        assertNotNull(message);
        assertEquals(messageID, message.getMessageID());
    }
    @Test
    public void testGetChatMessagesByUser() {
        int userID = 2;
        List<ChatMessage> chatMessages = chatMessageService.getChatMessagesByUser(userID);
        System.out.println("Chat Messages for User with ID " + userID + ":");
        for (ChatMessage chatMessage : chatMessages) {
            System.out.println(Arrays.toString(chatMessage.getPhoto()));
        }// 添加断言判断
        assertNotNull(chatMessages);
    }
    @Test
    public void testGetChatMessagesByChatRoom() {
        int chatRoomID = 2;
        List<ChatMessage> chatMessages = chatMessageService.getChatMessagesByChatRoom(chatRoomID);
        System.out.println("Chat Messages for Chat Room with ID " + chatRoomID + ":");
        for (ChatMessage chatMessage : chatMessages) {
            System.out.println(Arrays.toString(chatMessage.getPhoto()));
        }
        assertNotNull(chatMessages);
    }
    @Test
    public void testGetAllChatMessages() {
        List<ChatMessage> chatMessages = chatMessageService.getAllChatMessages();
        System.out.println("All Chat Messages:");
        for (ChatMessage chatMessage : chatMessages) {
            System.out.println(chatMessage);
        }
        // 添加断言判断
        assertNotNull(chatMessages);
    }

    @Test
    public void testCreateChatMessage() {
        ChatMessage newChatMessage = new ChatMessage();
        newChatMessage.setSenderID(2);
        newChatMessage.setReceiverID(1);
        newChatMessage.setChatRoomID(5);
        newChatMessage.setMessageType("Text");
        newChatMessage.setMessageContent("Hello, this is a new message!");
        newChatMessage.setSendTime(new java.util.Date());
//        newChatMessage.setPhoto(null);
        boolean createSuccess = chatMessageService.createChatMessage(newChatMessage);
        System.out.println("Create success: " + createSuccess);
        // 添加断言判断
        assertTrue(createSuccess);
    }
    @Test
    public void testUpdateChatMessage() {
        int messageID = 1;
        ChatMessage messageToUpdate = chatMessageService.getChatMessageByID(messageID);
        if (messageToUpdate != null) {
            messageToUpdate.setMessageContent("Updated message content");
            boolean updateSuccess = chatMessageService.updateChatMessage(messageToUpdate);
            System.out.println("Update success: " + updateSuccess);
            // 添加断言判断
            assertTrue(updateSuccess);
        } else {
            System.out.println("Message with ID " + messageID + " not found.");
            fail("Failed to update message. Message not found.");
        }
    }

    @Test
    public void testDeleteChatMessage() {
        int messageID = 2;
        boolean deleteSuccess = chatMessageService.deleteChatMessage(messageID);
        System.out.println("Delete success: " + deleteSuccess);
        // 添加断言判断
        assertTrue(deleteSuccess);
    }

    @Test
    public void testGetChatMessageBySenderIDAndReceiverID() {
        int senderID = 1001;
        int receiverID = 2001;
        ChatMessage chatMessage = chatMessageService.getChatMessageBySenderIDAndReceiverID(senderID, receiverID);
        System.out.println("Chat Message with Sender ID " + senderID + " and Receiver ID " + receiverID + ": " + chatMessage);
        // 添加断言判断
        assertNotNull(chatMessage);
    }
}
